<?php

namespace plugins\device\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\CommonUntil;
use plugins\device\model\DeviceModel;
use think\Db;

/**
 * 部门
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 设备列表
     */
    public function index()
    {
        return $this->fetch();
    }

    public function add_page()
    {
        return $this->fetch();
    }

    public function edit_page()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }


    /**
     * 设备分页列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $deviceModel = new DeviceModel();
        $pageSize = '10';
        if (!empty($param['pageSize'])) {
            $pageSize = $param['pageSize'];
        }
        $where = [];
        if (!empty($param['name'])) {
            $where['name'] = ['like', '%' . $param['name'] . '%'];
        }
        $list = $deviceModel->where($where)->paginates($pageSize)->each(function ($item) use ($deviceModel) {
            return $deviceModel->getDetail($item);
        });
        return api_json_echo(true, '查询成功', $list);
    }

    /**
     * 获取详情
     */
    public function getDetail()
    {
        $param = $this->request->param();
        $deviceModel = new DeviceModel();
        if (empty($param['id'])) {
            return api_json_echo(false, '传参错误');
        }
        $info = $deviceModel->where('id', $param['id'])->find();
        return api_json_echo(true, '查询成功', $info);
    }


    /**
     * 添加设备
     */
    public function add()
    {
        $param = $this->request->param();
        $deviceModel = new DeviceModel();
        if (empty($param['serial_number']) || empty($param['name']) || empty($param['ip']) || empty($param['address_id'])                                                                                                                 ) {
            return api_json_echo(false, '传参错误');
        }
        $is_exit = $deviceModel->where('serial_number', $param['serial_number'])->count();
        if($is_exit>0){
            return api_json_echo(false, '该设备编号已存在');
        }
        if (is_array($param['address_id'])) {
            $address = end($param['address_id']);
            $param['address_id'] = $address;
        }
        $param['create_uid'] = cmf_get_current_admin_id();
        $param['edit_time'] = time();
        $param['add_time'] = time();
        $re = $deviceModel->strict(false)->insert($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 编辑设备
     */
    public function edit()
    {
        $param = $this->request->param();
        $deviceModel = new DeviceModel();
        if (empty($param['id']) || empty($param['serial_number']) || empty($param['name']) || empty($param['ip']) || empty($param['address_id'])) {
            return api_json_echo(false, '传参错误');
        }
        $device = $deviceModel->where('id', $param['id'])->find();
        if($device['serial_number'] != $param['serial_number']){
            $is_exit = $deviceModel->where('serial_number', $param['serial_number'])->count();
            if($is_exit>0){
                return api_json_echo(false, '该设备编号已存在');
            }
        }
        if (is_array($param['address_id'])) {
            $address = end($param['address_id']);
            $param['address_id'] = $address;
        }
        $param['edit_time'] = time();
        $re = $device->strict(false)->where('id', $param['id'])->update($param);
        if ($re === null) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 删除
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['id'])) {
            return api_json_echo(false, '传值错误');
        }
        $deviceModel = new DeviceModel();
        $re = $deviceModel->where('id', $param['id'])->delete();
        if (empty($re)) {
            return api_json_echo(false, '删除失败');
        }
        return api_json_echo(true, '删除成功');
    }

    /**
     * 导出excel
     */
    public function exportExcel()
    {
        $commonUntil = new CommonUntil();

        $deviceModel = new DeviceModel();
        $where = [];
        $list = $deviceModel->where($where)->select()->each(function ($item) use ($deviceModel) {
            return $deviceModel->getDetail($item);
        });
        $expDataList = [];
        foreach ($list as $item){
            $item_list = [];
            $item_list[] = $item['serial_number'];
            $item_list[] = $item['name'];
            $item_list[] = $item['ip'];
            $item_list[] = $item['lng'];
            $item_list[] = $item['lat'];
            $item_list[] = $item['address_name'];
            $item_list[] = $item['status_str'];
            $expDataList[] = $item_list;
        }
        $expTitle = date('Y-m-d H:i').'-设备导出';
        $expHeadArr = ['设备编号','设备名称','设备ip','经度','维度','所在位置','状态'];
        $filePath = $commonUntil->exportExcelUntil($expTitle,$expHeadArr,$expDataList);
        return api_json_echo(true, '导出成功', zy_domain() . '/upload/' . $filePath);
    }
}
